#include "serialdata.h"
#include <QDebug>
#include <stdio.h>
#include <math.h>

#include "../logs/printlog.h"
#include "../gipos/gpiodata.h"


#if SERIAL_TEST
//#include<wiringPi.h>
//#include<wiringSerial.h>
//#include <unistd.h>

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>

#include <errno.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>

#endif



SerialData::SerialData(QObject *parent) : QThread(parent)
  , m_fd(-1)
  , m_isReadData(false)
  , m_isPotency(false)
  , m_isRun(false)
  , m_dataTimer(NULL)
  , m_serial(NULL)
  , m_isRecvData(false)
{
    m_currentData.clear();

    // m_serial = new QSerialPort(this);

    // connect(m_serial, SIGNAL( readyRead() ), this, SLOT( onSerialPortReadyRead() ) );

    m_dataTimer = new QTimer(this);
    connect(m_dataTimer ,SIGNAL(timeout() ) ,this ,SLOT(onReadTimeData() ) );
    // setup();
}

SerialData::~SerialData()
{
#if SERIAL_TEST
    m_isRun = false;
    //    if(m_serial != NULL){
    //        m_serial->close();
    //        delete  m_serial;
    //    }
    //    if(m_fd >= 0){
    //        serialClose(m_fd);
    //    }

#endif
}

SerialData *SerialData::gestance()
{
    static SerialData   serialData;

    return  &serialData;
}

bool SerialData::initSerial()
{
    qDebug()<<"SerialData::initSerial"<<"init";
    int fd; //Linux 的思想是：将一切IO设备，都看做 文件，fd就是代表串口抽象出来的文件
    //#if SERIAL_TEST
    //    if((fd = serialOpen("/dev/ttyAMA0",9600))==-1)    //初始化串口，波特率9600
    //    {

    //        qDebug()<<"SerialData::initSerial"<<"failed";
    //        QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" failed";
    //        PrintLog::gestance()->printInfor(logStr);
    //        return false;

    //    }
    //#endif


    fd= open("/dev/ttyAMA0" ,O_RDWR | O_NOCTTY | O_NONBLOCK);
    if(fd < 0){
        qDebug()<<"SerialData::initSerial"<<"failed";
        QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" failed";
        PrintLog::gestance()->printInfor(logStr);
        return false;
    }

    struct termios opt;

    if (tcgetattr(fd, &opt) != 0)
    {
        qDebug()<<"SerialData::initSerial"<<"opt failed";
        QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents opt ="+" failed";
        PrintLog::gestance()->printInfor(logStr);
    }

    fcntl(fd, F_SETFL, 0);
    //tcgetattr(fd,oldtio); //save current serial port settings
    bzero(&opt, sizeof(opt)); // clear struct for new port settings
    //configure the serial port;
    int speed = 9600;

    cfsetispeed(&opt, B9600);
    cfsetospeed(&opt, B9600);


    opt.c_cflag |= CLOCAL | CREAD;
    /*8N1*/
    opt.c_cflag &= ~CSIZE; /* Mask the character size bits */
    opt.c_cflag |= CS8; /* Select 8 data bits */
    opt.c_cflag &= ~PARENB;
    opt.c_cflag &= ~CSTOPB;
    opt.c_cflag &= ~CRTSCTS;//disable hardware flow control;
    opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);/*raw input*/
    opt.c_oflag &= ~OPOST; /*raw output*/
    tcflush(fd, TCIFLUSH);//clear input buffer
    opt.c_cc[VTIME] = 1; /* inter-character timer unused */
    opt.c_cc[VMIN] = 0; /* blocking read until 0 character arrives */
    if (tcsetattr(fd, TCSANOW, &opt) != 0)
    {
        qDebug()<<"SerialData::initSerial"<<("Cannot set the serial port parameters");
        return false;
    }


    //    cfsetispeed(&opt,B9600);
    //    cfsetospeed(&opt,B9600);

    //    opt.c_cflag &= ~INPCK;
    //    opt.c_cflag |=(CLOCAL | CREAD);

    //    opt.c_lflag &=~(ICANON | ECHO | ECHOE | ISIG);

    //    opt.c_oflag &= ~OPOST;
    //    opt.c_oflag &= ~(ONLCR | OCRNL);

    //    opt.c_iflag &= ~(ICRNL |  INLCR);

    //    opt.c_iflag &= ~(IXON | IXOFF | IXANY);

    //    opt.c_cflag &= ~ CSIZE;     //字符长度掩码,取值为 CS5, CS6, CS7, 或 CS8,加~就是无
    //    opt.c_cflag |=  CS8;        //数据宽度是8bit
    //    opt.c_cflag &= ~ CSTOPB;    //CSTOPB设置两个停止位，而不是一个,加~就是设置一个停止位
    //    opt.c_cflag &= ~ PARENB;

    //    opt.c_cc[VTIME] = 0 ;   //等待数据时间(10秒的倍数),每个单位是0.1秒  20就是2秒
    //    opt.c_cc[VMIN] = 255 ;    //最少可读数据,非规范模式读取时的最小字符数，设为0则为非阻塞，如果设为其它值则阻塞，直到读到到对应的数据,就像一个阀值一样，比如设为8，如果只接收到3个数据，那么它是不会返回的，只有凑齐8个数据后一齐才READ返回，阻塞在那儿
    //    tcflush(fd, TCIOFLUSH);         //刷串口清缓存
    //    tcsetattr(fd, TCSANOW, &opt);   //设置终端控制属性,TCSANOW：不等数据传输完毕就立即改变属性

    m_fd = fd;










    //    if(m_serial == NULL){
    //        qDebug()<<"SerialData::initSerial"<<"failed";
    //        QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" failed";
    //        PrintLog::gestance()->printInfor(logStr);
    //        return false;
    //    }
    //创建串口对象

    //    //设置串口名
    //    m_serial->setPortName("/dev/ttyAMA0");
    //    //设置波特率
    //    m_serial->setBaudRate(QSerialPort::Baud9600);
    //    //设置数据位数
    //    m_serial->setDataBits(QSerialPort::Data8);
    //    //设置奇偶校验
    //    m_serial->setParity(QSerialPort::NoParity);
    //    //设置停止位
    //    m_serial->setStopBits(QSerialPort::OneStop);
    //    //设置流控制 QSerialPort::UnknownFlowControl
    //    m_serial->setFlowControl(QSerialPort::NoFlowControl);

    //    //打开串口
    //    if( !m_serial->open(QIODevice::ReadWrite)){
    //        qDebug()<<"SerialData::initSerial"<<" open failed";
    //        QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents open="+" failed";
    //        PrintLog::gestance()->printInfor(logStr);
    //        return false;
    //    }


    qDebug()<<"SerialData::initSerial"<<"m_fd =="<<m_fd;
    QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" m_fd=="+QString::number(m_fd);
    PrintLog::gestance()->printInfor(logStr);
    unsigned char tempa[30];

    //    memset(tempa,0x00,30);
    //    tempa[0]=0xFF;
    //    tempa[1]=0xD7;
    //    tempa[2]=0x19;
    //    tempa[3]=0x00;
    //    tempa[4]=0xC8;
    //    tempa[5]=0x02;
    //    tempa[6]=0x01;
    //    tempa[7]=0x00;
    //    tempa[8]=funCheckSum(tempa,9);

    //    QByteArray inforas((char *)tempa,9);

    //    logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" infora=="+inforas.toHex();
    //    PrintLog::gestance()->printInfor(logStr);
    //    writeSerialData(inforas);


    // sleep(1);
#if 0
    memset(tempa,0x00,30);
    tempa[0]=0xFF;
    tempa[1]=0x01;
    tempa[2]=0x78;
    tempa[3]=0x40;
    tempa[4]=0x00;
    tempa[5]=0x00;
    tempa[6]=0x00;
    tempa[7]=0x00;
    tempa[8]=funCheckSum(tempa,9);


    QByteArray infora((char *)tempa,9);
#endif
    memset(tempa,0x00,30);
    tempa[0]=0x01;
    tempa[1]=0x03;
    tempa[2]=0x00;
    tempa[3]=0x1F;
    tempa[4]=0x00;
    tempa[5]=0x01;

    QByteArray infora((char *)tempa,6);

    unsigned short itema = m_crc16class.crc16((unsigned char *)tempa,6);
    unsigned short itemb =itema;
    unsigned short itemc =itema;
    char  itemd =(char)(itemb >> 8);
    char  iteme =(char)(itemc);

    infora.append(iteme);
    infora.append(itemd);
    //    tempa[6]=0x00;
    //    tempa[7]=0x00;
    //    tempa[8]=funCheckSum(tempa,9);




    logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" infora=="+infora.toHex();
    PrintLog::gestance()->printInfor(logStr);
    writeSerialData(infora);
    //readSerialData();
    m_isRun=true;
    m_currentData.clear();
    m_isReadData = true;
    start();


    //emit   sigSendStatus(8);
    return true;
}

bool SerialData::initSecond()
{
    qDebug()<<"SerialData::initSerial"<<"init";
    int fd; //Linux 的思想是：将一切IO设备，都看做 文件，fd就是代表串口抽象出来的文件
    //#if SERIAL_TEST
    //    if((fd = serialOpen("/dev/ttyAMA0",9600))==-1)    //初始化串口，波特率9600
    //    {

    //        qDebug()<<"SerialData::initSerial"<<"failed";
    //        QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" failed";
    //        PrintLog::gestance()->printInfor(logStr);
    //        return false;

    //    }
    //#endif


    fd= open("/dev/ttyAMA0" ,O_RDWR | O_NOCTTY | O_NONBLOCK);
    if(fd < 0){
        qDebug()<<"SerialData::initSerial"<<"failed";
        QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" failed";
        PrintLog::gestance()->printInfor(logStr);
        return false;
    }

    struct termios opt;

    if (tcgetattr(fd, &opt) != 0)
    {
        qDebug()<<"SerialData::initSerial"<<"opt failed";
        QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents opt ="+" failed";
        PrintLog::gestance()->printInfor(logStr);
    }

    fcntl(fd, F_SETFL, 0);
    //tcgetattr(fd,oldtio); //save current serial port settings
    bzero(&opt, sizeof(opt)); // clear struct for new port settings
    //configure the serial port;
    int speed = 9600;

    cfsetispeed(&opt, B9600);
    cfsetospeed(&opt, B9600);


    opt.c_cflag |= CLOCAL | CREAD;
    /*8N1*/
    opt.c_cflag &= ~CSIZE; /* Mask the character size bits */
    opt.c_cflag |= CS8; /* Select 8 data bits */
    opt.c_cflag &= ~PARENB;
    opt.c_cflag &= ~CSTOPB;
    opt.c_cflag &= ~CRTSCTS;//disable hardware flow control;
    opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);/*raw input*/
    opt.c_oflag &= ~OPOST; /*raw output*/
    tcflush(fd, TCIFLUSH);//clear input buffer
    opt.c_cc[VTIME] = 1; /* inter-character timer unused */
    opt.c_cc[VMIN] = 0; /* blocking read until 0 character arrives */
    if (tcsetattr(fd, TCSANOW, &opt) != 0)
    {
        qDebug()<<"SerialData::initSerial"<<("Cannot set the serial port parameters");
        return false;
    }


    //    cfsetispeed(&opt,B9600);
    //    cfsetospeed(&opt,B9600);

    //    opt.c_cflag &= ~INPCK;
    //    opt.c_cflag |=(CLOCAL | CREAD);

    //    opt.c_lflag &=~(ICANON | ECHO | ECHOE | ISIG);

    //    opt.c_oflag &= ~OPOST;
    //    opt.c_oflag &= ~(ONLCR | OCRNL);

    //    opt.c_iflag &= ~(ICRNL |  INLCR);

    //    opt.c_iflag &= ~(IXON | IXOFF | IXANY);

    //    opt.c_cflag &= ~ CSIZE;     //字符长度掩码,取值为 CS5, CS6, CS7, 或 CS8,加~就是无
    //    opt.c_cflag |=  CS8;        //数据宽度是8bit
    //    opt.c_cflag &= ~ CSTOPB;    //CSTOPB设置两个停止位，而不是一个,加~就是设置一个停止位
    //    opt.c_cflag &= ~ PARENB;

    //    opt.c_cc[VTIME] = 0 ;   //等待数据时间(10秒的倍数),每个单位是0.1秒  20就是2秒
    //    opt.c_cc[VMIN] = 255 ;    //最少可读数据,非规范模式读取时的最小字符数，设为0则为非阻塞，如果设为其它值则阻塞，直到读到到对应的数据,就像一个阀值一样，比如设为8，如果只接收到3个数据，那么它是不会返回的，只有凑齐8个数据后一齐才READ返回，阻塞在那儿
    //    tcflush(fd, TCIOFLUSH);         //刷串口清缓存
    //    tcsetattr(fd, TCSANOW, &opt);   //设置终端控制属性,TCSANOW：不等数据传输完毕就立即改变属性

    m_fd = fd;










    //    if(m_serial == NULL){
    //        qDebug()<<"SerialData::initSerial"<<"failed";
    //        QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" failed";
    //        PrintLog::gestance()->printInfor(logStr);
    //        return false;
    //    }
    //创建串口对象

    //    //设置串口名
    //    m_serial->setPortName("/dev/ttyAMA0");
    //    //设置波特率
    //    m_serial->setBaudRate(QSerialPort::Baud9600);
    //    //设置数据位数
    //    m_serial->setDataBits(QSerialPort::Data8);
    //    //设置奇偶校验
    //    m_serial->setParity(QSerialPort::NoParity);
    //    //设置停止位
    //    m_serial->setStopBits(QSerialPort::OneStop);
    //    //设置流控制 QSerialPort::UnknownFlowControl
    //    m_serial->setFlowControl(QSerialPort::NoFlowControl);

    //    //打开串口
    //    if( !m_serial->open(QIODevice::ReadWrite)){
    //        qDebug()<<"SerialData::initSerial"<<" open failed";
    //        QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents open="+" failed";
    //        PrintLog::gestance()->printInfor(logStr);
    //        return false;
    //    }

    qDebug()<<"SerialData::initSerial"<<"m_fd =="<<m_fd;
    QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" m_fd=="+QString::number(m_fd);
    PrintLog::gestance()->printInfor(logStr);
    char tempa[30];
    memset(tempa,0x00,30);
    tempa[0]=0x01;
    tempa[1]=0x03;
    tempa[2]=0x00;
    tempa[3]=0x00;
    tempa[4]=0x00;
    tempa[5]=0x01;
    m_isRun=true;
    m_currentData.clear();
    m_isReadData = true;

    QByteArray infora(tempa,6);

    logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" infora=="+infora.toHex();
    PrintLog::gestance()->printInfor(logStr);
    writeSerialData(infora);

    return true;
    //readSerialData();
}

void SerialData::setup()
{
#if SERIAL_TEST
    //    if(-1==wiringPiSetup())
    //    {
    //        QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" wiringPiSetup == fail";
    //        PrintLog::gestance()->printInfor(logStr);
    //        exit(-1);
    //    }
    // pinMode(SERIAL485_ENALE_1,OUTPUT);
    // digitalWrite(SERIAL485_ENALE_1,LOW);
#endif
}

void SerialData::readSerialData()
{
    m_currentData.clear();
    m_isReadData = true;

    // start();
    //run();
    //    if(m_dataTimer != NULL){
    //        m_dataTimer->start(30);
    //    }
}

void SerialData::readCoPotency()
{
    m_isRecvData = true;
    char tempa[30];
    memset(tempa,0x00,30);
    tempa[0]=0x01;
    tempa[1]=0x03;
    tempa[2]=0x00;
    tempa[3]=0x00;
    tempa[4]=0x00;
    tempa[5]=0x01;
    QByteArray infora(tempa,6);
    unsigned short itema = m_crc16class.crc16((unsigned char *)tempa,6);
    unsigned short itemb =itema;
    unsigned short itemc =itema;
    char  itemd =(char)(itemb >> 8);
    char  iteme =(char)(itemc);

    infora.append(iteme);
    infora.append(itemd);
    QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" infora=="+infora.toHex();
    PrintLog::gestance()->printInfor(logStr);
    // m_isPotency = true;
    //readSerialData();
    writeSerialData(infora);


    //    static bool isInit = true;
    //    if(!isInit){
    //        emit  sigSendPotency(6);
    //    }else{
    //        emit  sigSendPotency(38);
    //    }
    //    isInit = !isInit;
}

void SerialData::writeSerialData(QByteArray infors)
{

    QByteArray item = infors;
    //    unsigned short itema = m_crc16class.crc16((unsigned char *)item.data(),item.size());
    //    unsigned short itemb =itema;
    //    unsigned short itemc =itema;
    //    char  itemd =(char)(itemb >> 8);
    //    char  iteme =(char)(itemc);
    //    item.append(iteme);
    //    item.append(itemd);
    qDebug()<<"SerialData::writeSerialData item=="<<item.toHex();
    //    if(m_serial != NULL){
    //        int lensa =  m_serial->write(item);
    //        // m_serial->waitForBytesWritten(100);
    //        // m_serial->waitForBytesWritten(-1);
    //        // int lenTmepa = m_serial->bytesAvailable();
    //        //  qDebug()<<"SerialData::writeSerialData::run "<<"lenTmepa ="<<lenTmepa;
    //        // QByteArray buffer = m_serial->readAll();
    //        //  qDebug()<<"SerialData::writeSerialData::run "<<"buffer ="<<buffer.toHex();
    //        //m_serial->flush();
    //        qDebug()<<"SerialData::writeSerialData lensa=="<<lensa;
    //    }
#if SERIAL_TEST
    // digitalWrite(SERIAL485_ENALE_1,HIGH);
    //  msleep(3);
    int lens = write (m_fd,(void *)item.data(),item.size());
    QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" lens=="+lens;
    PrintLog::gestance()->printInfor(logStr);
    tcflush(m_fd,TCIOFLUSH);
    //    serialFlush(m_fd);
    //    int lens = 0;
    //    for(int i = 0; i <item.size();i++ ){
    //        char aitme = item.at(i);
    //        serialPutchar(m_fd,aitme);
    //        lens++;
    //    }
    //    serialFlush(m_fd);

    qDebug()<<"SerialData::writeSerialData lens=="<<lens;
    //flush(m_fd);
    // serialFlush(m_fd);
    //  msleep(300);
    //  digitalWrite(SERIAL485_ENALE_1,LOW);
#endif
}

unsigned char SerialData::funCheckSum(unsigned char *i, unsigned char ln)
{
    unsigned char j=0;
    unsigned char stempg=0;
    i+=1;
    for(j=0;j <(ln -2) ;j++){
        stempg+=*i;
        i++;
    }
    stempg=(~stempg)+1;

    return stempg;
}

void SerialData::onReadTimeData()
{
    if(m_dataTimer != NULL){
        m_dataTimer->stop();
    }
    char tempa[30];
    memset(tempa,0x00,30);
    tempa[0]=0x01;
    tempa[1]=0x03;
    QByteArray infora(tempa,2);
    int indexa = 0;
    int conta = 0;
#if SERIAL_TEST
    char lens = 0;
    char buf[1024];
    m_currentData.clear();
    memset(buf,0x00,1024);
    int numa =0;

    //  lens =serialDataAvail(m_fd);
    //     qDebug()<<"SerialData::run "<<"lens ="<<lens;
    //lens= read(m_fd,(void * )buf ,1024);
    lens = 0;//serialGetchar(m_fd);

    if( lens >= 0){
        qDebug()<<"SerialData::run "<<"lens =1="<<lens;
        QByteArray item(buf,lens);
        memset(buf,0x00,1024);
        m_currentData.append(item);
        qDebug()<<"SerialData::run "<<"m_currentData ="<<m_currentData.toHex();
        indexa = m_currentData.indexOf(tempa);
        qDebug()<<"SerialData::run "<<"indexa ="<<indexa;
        QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" m_currentData=="+m_currentData.toHex();
        PrintLog::gestance()->printInfor(logStr);
        if( indexa >= 0){
            char itmb = m_currentData.at(indexa+2);
            qDebug()<<"SerialData::run "<<"itmb ="<<itmb;

            if((indexa+itmb+2) <= m_currentData.length() ){
                char itmc =  m_currentData.at(indexa+3);
                char itmd =  m_currentData.at(indexa+4);
                qDebug()<<"SerialData::run "<<"itmc ="<<itmc;
                qDebug()<<"SerialData::run "<<"itmd ="<<itmd;

                conta =itmc;
                conta =(conta << 8);
                conta+=itmd;
                m_currentData.clear();
                m_isReadData = false;
                qDebug()<<"SerialData::run "<<"conta ="<<conta;

                if(m_isPotency){
                    emit sigSendPotency(conta);
                }else{
                    emit sigSendStatus(conta);
                }
                m_isReadData = false;

            }
        }else{
            m_currentData.clear();


            m_isReadData = false;
            m_currentData.clear();
            emit sigSendStatus(-1);

        }

    }else{


        m_isReadData = false;
        m_currentData.clear();
        emit sigSendStatus(-1);

    }

#endif
}

void SerialData::onSerialPortReadyRead()
{
    qDebug()<<"---SerialData::onSerialPortReadyRead---";
    char tempa[30];
    memset(tempa,0x00,30);
    tempa[0]=0x01;
    tempa[1]=0x03;
    int conta = 0;
    char buf[1024];
    m_currentData.clear();
    memset(buf,0x00,1024);
    if(m_serial != NULL){
        //        int lensa = m_serial->read(buf,1024);
        //        if(lensa > 0){
        //            qDebug()<<"SerialData::erialData::run "<<"lensa ="<<lensa;
        //   QByteArray buffer(buf);
        int lenTmepa = m_serial->bytesAvailable();
        qDebug()<<"SerialData::erialData::run "<<"lenTmepa ="<<lenTmepa;
        QByteArray buffer = m_serial->readAll();
        qDebug()<<"SerialData::erialData::run "<<"buffer ="<<buffer.toHex();
        int indexa = 0;
        if(buffer.length() > 0){

            if(m_isReadData){

                m_currentData.append(buffer);
                qDebug()<<"SerialData::erialData::run "<<"m_currentData ="<<m_currentData.toHex();
                indexa = m_currentData.indexOf(tempa);
                qDebug()<<"SerialData::run "<<"indexa ="<<indexa;
                QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" m_currentData=="+m_currentData.toHex();
                PrintLog::gestance()->printInfor(logStr);
                if( indexa >= 0){
                    char itmb = m_currentData.at(indexa+2);
                    qDebug()<<"SerialData::run "<<"itmb ="<<itmb;

                    if((indexa+itmb+2) <= m_currentData.length() ){
                        char itmc =  m_currentData.at(indexa+3);
                        char itmd =  m_currentData.at(indexa+4);
                        qDebug()<<"SerialData::run "<<"itmc ="<<itmc;
                        qDebug()<<"SerialData::run "<<"itmd ="<<itmd;

                        conta =itmc;
                        conta =(conta << 8);
                        conta+=itmd;
                        m_currentData.clear();
                        m_isReadData = false;
                        qDebug()<<"SerialData::run "<<"conta ="<<conta;

                        if(m_isPotency){
                            emit sigSendPotency(conta);
                        }else{
                            emit sigSendStatus(conta);
                        }
                        m_isReadData = false;

                    }
                }else{


                }

            }
        }
    }

}

void SerialData::run()
{
    static int indexint = 0;
    qDebug()<<"---SerialData::run---indexint=="<<indexint;
    indexint++;
    char tempa[30];
    memset(tempa,0x00,30);
    tempa[0]=0x01;
    tempa[1]=0x03;
    tempa[2]=0x02;
    QByteArray infora(tempa,3);
    int indexa = 0;
    int conta = 0;
#if SERIAL_TEST
    char lens = 0;
    char buf[1024];
    m_currentData.clear();
    memset(buf,0x00,1024);
    int numa =0;

    int currentIndexa =0;

    int currentIndexb =0;
#if 0
    while (m_isRun ) {
        //  lens =serialDataAvail(m_fd);
        //     qDebug()<<"SerialData::run "<<"lens ="<<lens;

        lens=(int) read(m_fd,(void * )buf ,1024);
        //lens = serialGetchar(m_fd);
        qDebug()<<"SerialData::run "<<"lens =1s="<<lens;
        qDebug()<<"SerialData::run "<<"lens =1saa="<<QString::number( lens)<<" ##==!";
        if( lens > 0){
            qDebug()<<"SerialData::run "<<"lens =1="<<lens;
            QByteArray item(buf,lens);
            memset(buf,0x00,1024);
            m_currentData.append(item);
            //     m_currentData.append(lens);
            QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" infora=="+infora.toHex();
            PrintLog::gestance()->printInfor(logStr);
            qDebug()<<"SerialData::run "<<"m_currentData ="<<m_currentData.toHex();
            indexa = m_currentData.indexOf(infora,indexa);
            qDebug()<<"SerialData::run "<<"indexa ="<<indexa;
            logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" m_currentData=="+m_currentData.toHex();
            PrintLog::gestance()->printInfor(logStr);
            logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" indexa=="+QString::number(indexa);
            PrintLog::gestance()->printInfor(logStr);
            if( indexa >= 0){
                char itmb = 7;//m_currentData.at(indexa+2);
                qDebug()<<"SerialData::run "<<"itmb ="<<itmb;
                QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" itmb=="+QString::number(itmb);
                PrintLog::gestance()->printInfor(logStr);
                if((indexa+itmb+1) <= m_currentData.length() ){
                    qDebug()<<"SerialData::run "<<"indexa+itmb+1 ="<<indexa+itmb+1;
                    qDebug()<<"SerialData::run "<<"indexa+itmb+1 ="<<indexa+itmb+1;
                    //  unsigned char itemfs =funCheckSum((unsigned char *)(m_currentData.data() +indexa) , 9);
                    //  QByteArray aatemp((char * )&itemfs,1);
                    //  qDebug()<<"SerialData::run "<<"aatemp ="<<aatemp.toHex();
                    //   QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" aatemp.toHex()=="+aatemp.toHex();
                    //  PrintLog::gestance()->printInfor(logStr);
                    // if(m_currentData.at(indexa+itmb+1) == itemfs ){
                    char itmc =  m_currentData.at(indexa+6);
                    char itmd =  m_currentData.at(indexa+7);
                    char itmf = 0;// m_currentData.at(indexa+3);
                    int pows = pow(10,itmf+3);

                    qDebug()<<"SerialData::run "<<"itmc ="<<itmc;
                    qDebug()<<"SerialData::run "<<"itmd ="<<itmd;
                    logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" pows=="+QString::number(pows);
                    PrintLog::gestance()->printInfor(logStr);
                    conta =itmc;
                    conta =(conta << 8);
                    conta+=itmd;
                    conta = conta ;
                    logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" conta=="+QString::number(conta);
                    PrintLog::gestance()->printInfor(logStr);
                    double contc = 1.0 * conta /pows;
                    m_currentData.clear();
                    indexa = 0;
                    logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" contc=="+QString::number(contc);
                    PrintLog::gestance()->printInfor(logStr);

                    qDebug()<<"SerialData::run "<<"conta ="<<conta;
                    qDebug()<<"SerialData::run "<<"contc ="<<contc;

                    emit sigSendPotency(contc);

                    //                    }else{
                    //                        indexa++;
                    //                    }



                }else{
                    if(indexa >= m_currentData.length()){
                        m_currentData.clear();
                        indexa =0;
                    }
                }
            }else{
                if(m_currentData.length() >= 9){
                    m_currentData.clear();
                    indexa =0;
                }
            }

        }else{
            msleep(10);
        }

    }
#endif

    while (m_isRun ) {
        //  lens =serialDataAvail(m_fd);
        //     qDebug()<<"SerialData::run "<<"lens ="<<lens;

        lens=(int) read(m_fd,(void * )buf ,1024);
        //lens = serialGetchar(m_fd);
        qDebug()<<"SerialData::run "<<"lens =1s="<<lens;
        qDebug()<<"SerialData::run "<<"lens =1saa="<<QString::number( lens)<<" ##==!";
        if( lens > 0){
            qDebug()<<"SerialData::run "<<"lens =1="<<lens;
            currentIndexa = 0;
            currentIndexb = 0;
            m_isRecvData = false;
            QByteArray item(buf,lens);
            memset(buf,0x00,1024);
            m_currentData.append(item);
            //     m_currentData.append(lens);
            QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" infora=="+infora.toHex();
            PrintLog::gestance()->printInfor(logStr);
            qDebug()<<"SerialData::run "<<"m_currentData ="<<m_currentData.toHex();
            indexa = m_currentData.indexOf(infora,indexa);
            qDebug()<<"SerialData::run "<<"indexa ="<<indexa;
            logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" m_currentData=="+m_currentData.toHex();
            PrintLog::gestance()->printInfor(logStr);
            logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" indexa=="+QString::number(indexa);
            PrintLog::gestance()->printInfor(logStr);
            if( indexa >= 0){
                char itmb = 6;//m_currentData.at(indexa+2);
                qDebug()<<"SerialData::run "<<"itmb ="<<itmb;
                QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" itmb=="+QString::number(itmb);
                PrintLog::gestance()->printInfor(logStr);
                if((indexa+itmb+1) <= m_currentData.length() ){

                    qDebug()<<"SerialData::run "<<"indexa+itmb+1 ="<<indexa+itmb+1;
                    qDebug()<<"SerialData::run "<<"indexa+itmb+1 ="<<indexa+itmb+1;
                    //  unsigned char itemfs =funCheckSum((unsigned char *)(m_currentData.data() +indexa) , 9);
                    //  QByteArray aatemp((char * )&itemfs,1);
                    //  qDebug()<<"SerialData::run "<<"aatemp ="<<aatemp.toHex();
                    //   QString logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" aatemp.toHex()=="+aatemp.toHex();
                    //  PrintLog::gestance()->printInfor(logStr);
                    // if(m_currentData.at(indexa+itmb+1) == itemfs ){
                    char itmc =  m_currentData.at(indexa+3);
                    char itmd =  m_currentData.at(indexa+4);
                    char itmf = 0;// m_currentData.at(indexa+3);
                    int pows = pow(10,itmf+3);

                    qDebug()<<"SerialData::run "<<"itmc ="<<itmc;
                    qDebug()<<"SerialData::run "<<"itmd ="<<itmd;
                    logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" pows=="+QString::number(pows);
                    PrintLog::gestance()->printInfor(logStr);
                    conta =itmc;
                    conta =(conta << 8);
                    conta+=itmd;
                    conta = conta ;
                    logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" conta=="+QString::number(conta);
                    PrintLog::gestance()->printInfor(logStr);
                    double contc = 1.0 * conta /pows;
                    m_currentData.clear();
                    indexa = 0;
                    logStr= typeid(this).name()+QString(" %1").arg( __func__) +" -- "+ QString("%1").arg( __LINE__)  +" contents ="+" contc=="+QString::number(contc);
                    PrintLog::gestance()->printInfor(logStr);

                    qDebug()<<"SerialData::run "<<"conta ="<<conta;
                    qDebug()<<"SerialData::run "<<"contc ="<<contc;

                    emit sigSendPotency(contc);

                    //                    }else{
                    //                        indexa++;
                    //                    }



                }else{
                    if(indexa >= m_currentData.length()){
                        m_currentData.clear();
                        indexa =0;
                    }
                }
            }else{
                if(m_currentData.length() >= 7){
                    m_currentData.clear();
                    indexa =0;
                }
            }

        }else{
            msleep(10);
            if(m_isRecvData){
                currentIndexa++;
                if(currentIndexa >= 200){
                    if(GpioData::gestance()->getDevice() ==0){
                        GpioData::gestance()->openDevice();
                    }
                    currentIndexb++;
                }

                if(currentIndexb >= 100){
                    if(GpioData::gestance()->getDevice() ==1){
                        GpioData::gestance()->closeDevice();
                    }
                    currentIndexa = 0;
                    currentIndexb = 0;
                    m_isRecvData =false;
                }
            }
        }

    }



#endif




}
